var indexGlobal={
		menuClass:{
			'用户信息管理':'icon01','运营管理':'icon02','商品管理':'icon03','订单管理':'icon04','营销管理':'icon05','配送管理':'icon06','渠道商管理':'icon07',
			'用户管理':'icon08','客服管理':'icon09','财务管理':'icon10','系统管理':'icon11',
			'会员注册管理':'icon11','铁水联运补贴管理':'icon12','数据字典':'icon13','统计':'icon14','统计报表':'icon14'	
		},
		menuClass_new:{
			'源梦用户管理':'&#xe682;','系统管理':'&#xe642;','资讯管理':'&#xe632;',
			'运营管理':'&#xe635;','监控中心':'&#xe60f;','合作平台':'&#xe60e;',
			'日志管理':'&#xe642;','开放平台管理':'&#xe60e;','会员注册管理':'&#xe632;','铁水联运补贴管理':'&#xe632;','数据字典':'&#xe632;',
			'统计':'&#xe632;'	,'统计报表':'&#xe632;'	
		}
};
$(document).ready(function(){
	$(window).resize(function() { 
		var leftMenuHeight=getclientHeight()-110;
		$('#leftMenu').css('height',leftMenuHeight);
	});
	$('#leftMenu').css('height',getclientHeight()-110);
	//console.log("clientHeight:"+(getclientHeight()-110));
	var backOperatorId = $("#backOperatorId").val();
	$.post(mainDomain+'/menu/selectAllActivityMenu.action',{'backOperatorId':backOperatorId},function(rs){
		createLeftMenu(rs);
	},'json');
	//首页
	$("#toIndexPag,#toMainPag").click(function() {
		toIndexPag();
	});
	//我的资料
	$("#myInfo").click(function(){
		refreshCenter(mainDomain+"/backend/toUserInfo.action", "我的资料");
//		backendCommon.openInTabs(mainDomain+"/backend/toUserInfo.action", "我的资料");
	});
	//注销
	$("#logout").click(function() {
		logout();
	});
	
	//三个统计
	threeStatistics();
});

function createLeftMenu(rs){
	var l1=[];
	var l2={};
	$.each(rs,function(i,v){
		if(v.menuParentId==0 && v.isVisible){
			var menufont;
			if(v.menuName in indexGlobal.menuClass_new){
				menufont=indexGlobal.menuClass_new[v.menuName];
			}
			l1.push('<h1 sid="'+v.id+'"><p class="icon iconfont">'+menufont+'</p><a href="javascript:void(0);">'+v.menuName+'</a></h1>');
		}else{
			if(v.isVisible){
				if(v.menuParentId in l2){
					l2[v.menuParentId].push(v);
				}else{
					l2[v.menuParentId]=[];
					l2[v.menuParentId].push(v);
				}
			}
		}
	});
	var $ct=$('#leftMenu').append(l1.join(""));
	$.each(l2,function(k,v){
		var $p=$ct.children("h1[sid="+k+"]");
		var h2s=[];
		for(var i=0;i<v.length;i++){
			var menuUrl = '';
			if(v[i].menuType == 1){//系统内连接
				menuUrl = mainDomain+v[i].menuUrl;
			}else if(v[i].menuType == 2){//外部链接
				menuUrl = v[i].menuUrl;
			}
			h2s.push('<h2 menueId="'+v[i].id+'" menueName="'+v[i].menuName+'" pid="'+v[i].menuParentId+'" uri="'+menuUrl+'" style="display:none;"><a href="'+menuUrl+'" >'+v[i].menuName+'</a></h2>');
		}
		$p.after(h2s.join(''));
	});
	$ct.find("h1").toggle(
		function(){
			var sid=$(this).attr('sid');
			$ct.children("h2[pid="+sid+"]").show();
		},
		function(){
			var sid=$(this).attr('sid');
			$ct.children("h2[pid="+sid+"]").hide();
		}
	);
	$ct.delegate("h2",'click',function(e){
		 e.preventDefault();     
		var uri= $(this).attr('uri');
		refreshCenter(uri,$(this).attr('menueName'));
	});
	$ct.delegate("h3",'click',function(e){
		 e.preventDefault();     
		 $('#indexTab').tabs('close',$(this).attr('tabName'));
	});
}

function refreshCenter(url,menueName){
	var tab=$('#indexTab').tabs('getTab',menueName);
	var  content = '<iframe frameborder="0"  src="'+url+'" style="width:100%;height:100%;"></iframe>'; 
	if(tab){
		$('#indexTab').tabs('select',menueName);
		$('#indexTab').tabs('update', {
			tab: tab,
			options: {
				content:content
			}
		});
	}else{
		$('#indexTab').tabs('add',{    
		    title:menueName,    
		    content:content,    
		    closable:true,
		    tools:[{
				iconCls:'icon-reload',
				handler:function(){
					refreshCenter(url,menueName);
				}
			}]
		});
	}
}
function onIndexTabSelect(title,index){
	$('#leftMenu').find("h2").removeClass('select');
	$('#leftMenu').find('h2[menueName="'+title+'"]').addClass('select');
}

function loadError(XMLHttpRequest,status){
	$("#layoutCenter").html(XMLHttpRequest.responseText);
}

function getclientHeight(){
	var h=document.documentElement.clientHeight;
	return h;
}


//到首页
function toIndexPag(){
	top.location.href=mainDomain+'/backend/toIndex.action';
}
//注销退出
function logout(){
	var url=mainDomain+'/login/logout.action';
	$.post(url,{},
	function(result){
		if(result.success){
			top.location.href=mainDomain+'/login/index.action';
 		}else{
 			$.messager.alert('提示',"注销失败!",'info');
 		}
	},'json');
}

//三个统计
function threeStatistics(){
	var url=mainDomain+'/btsb/indexthree.action';
	$.post(url,{},
	function(result){
		if(result.success){
			$('#xlcount').html(result.data.xl);
			$('#cashcount').html(result.data.cash);
			$('#jdcount').html(result.data.jd);
 		}else{
 			$.messager.alert('提示',"首页统计失败!",'info');
 		}
	},'json');
}

var options = {
    tooltip : {
        trigger: 'axis',
        axisPointer : {            // 坐标轴指示器，坐标轴触发有效
            type : 'shadow'        // 默认为直线，可选为：'line' | 'shadow'
        }
    },
    toolbox: {
    	//显示策略，可选为：true（显示） | false（隐藏），默认值为false  
        show: true, 
        //magicType，动态类型切换，支持直角系下的折线图、柱状图、堆积、平铺转换  
        magicType: {show: true, type: ['line', 'bar']},  
        //restore，还原，复位原始图表  
        restore: {show: true}, 
    },	
    grid: {
        left: '3%',
        right: '4%',
        bottom: '3%',
        containLabel: true
    },
    xAxis: {
        data: []
    },
    yAxis: {
    },
    series: [ ]
 };

var xlurl = mainDomain + "/statistics/statisticsForIndex.action?flag=1";
var cashurl = mainDomain + "/statistics/statisticsForIndex.action?flag=2";
var xlChart = echarts.init(document.getElementById('xldiv'));
var cashChart = echarts.init(document.getElementById('cashdiv'));
xlChart.setOption(options);
cashChart.setOption(options);
load(xlurl, xlChart,"箱量(TEU)","季度箱量统计");
load(cashurl, cashChart,"金额(万元)","季度补贴金额统计");
/***加载数据***/
function load(url, myChart, dis, titlename){
    $.get(url).done(function (data) {
    	
    	var valueArr = [];
    	
    	$.each(data.categories, function(n, item){
    		valueArr.push(0);
        })
    	
    	var replaceindex;
        $.each(data.list, function(i, entry){
        	replaceindex = $.inArray(entry.sbsj,data.categories);
        	if(replaceindex!=-1){
        		valueArr.splice(replaceindex,1,entry.total);
        	}
        });
        
    	// 填入数据
        myChart.setOption({
        	title: {
                text: titlename
            },
            xAxis: {
                data: data.categories
            },
            yAxis : 
            	[ {
				name : dis
			}],
			series: 
            	[ {
				name : "2016",
				type : 'line',
				data : valueArr
			} ]
        });
    })
}